//
//  CYDiscountViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/4.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYDiscountViewController.h"
#
@interface CYDiscountViewController ()
@property (nonatomic, strong) UIScrollView *scrollview;
@property (nonatomic, strong) CYLabel *titleLBL;                       //优惠活动标题
@property (nonatomic, strong) CYLabel *detailTitleLBL;                 //优惠活动小标题
@property (nonatomic, strong) CYImageView *IMG;                        //优惠活动图片
@property (nonatomic, strong) CYLabel *time_titleLBL;                  //活动时间标题
@property (nonatomic, strong) CYLabel *time_startLBL;                  //活动开始时间
@property (nonatomic, strong) CYLabel *time_endLBL;                    //活动截止时间
@property (nonatomic, strong) CYLabel *activity_explain_titleLBL;      //活动说明标题
@property (nonatomic, strong) CYLabel *activity_explain_contentLBL;    //活动说明内容
@property (nonatomic, strong) CYLabel *activity_rule_titleLBL;         //活动规则标题
@property (nonatomic, strong) CYLabel *activity_rule_contentLBL;       //活动规则内容
@property (nonatomic, strong) CYLabel *activity_ownershipLBL;           //活动所有权说明
@property (nonatomic, strong) CYLabel *download_titleLBL;              //下载标题
@property (nonatomic, strong) CYLabel *download_appleLBL;              //苹果下载标题
@property (nonatomic, strong) CYLabel *download_androidLBL;            //安卓下载标题
@property (nonatomic, strong) CYButton *link_appleBtn;                 //苹果下载链接按钮
@property (nonatomic, strong) CYButton *link_androidBtn;               //安卓下载链接按钮
@property (nonatomic, strong) CYButton *registerBtn;                   //注册按钮
@property (nonatomic, strong) CYButton *rechargeBtn;                   //充值按钮
@property (nonatomic, strong) CYView *midLine;                         //注册按钮充值按钮中间的隔断线
@property (nonatomic, strong) CYView *topLine;                         //注册按钮充值按钮上面的阴影

@end

@implementation CYDiscountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.registerBtn.hidden = self.rechargeBtn.hidden = self.midLine.hidden = self.topLine.hidden = NO;
    // Do any additional setup after loading the view.
}
/*懒加载*/
- (UIScrollView *)scrollview{
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc]init];
        [self.view addSubview:_scrollview];
        
        [_scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(NAVIBAR_HEIGHT + STATUS_HEIGHT, 0, AL(PT(99)), 0));
        }];
    }
    return _scrollview;
}
- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:_model.active_title size:AL(PT(32)) weight:UIFontWeightBold];
        _titleLBL.textColor = ColorHex(0xff5b05);
        [_scrollview addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_scrollview).with.offset(AL(PT(30)));
            make.left.equalTo(_scrollview).with.offset(AL(PT(30)));
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYImageView *)IMG{
    if (!_IMG) {
        _IMG = [[CYImageView alloc]init];
        _IMG.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH - AL(PT(2 * 30)), 178 / 754.0 * (SCREEN_WIDTH - AL(PT(2 * 30))));
        _IMG.loadingView.hidden = NO;
        [_scrollview addSubview:_IMG];
        
        [_IMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(AL(PT(17)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(2 * 30)), 178 / 754.0 * (SCREEN_WIDTH - AL(PT(2 * 30)))));
        }];
    }
    
    return _IMG;
}

- (CYLabel *)detailTitleLBL{
    if (!_detailTitleLBL) {
        _detailTitleLBL = [[CYLabel alloc]initWithText:_model.activity_description size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        _detailTitleLBL.numberOfLines = 0;
        [_scrollview addSubview:_detailTitleLBL];
        
        [_detailTitleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_IMG.mas_bottom).with.offset(AL(PT(17)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo([_detailTitleLBL autoSizeWithWidth:(SCREEN_WIDTH - AL(30 * 2))]);
        }];
    }
    return _detailTitleLBL;
}


/*活动时间*/
- (CYLabel *)time_titleLBL{
    if (!_time_titleLBL) {
        _time_titleLBL = [[CYLabel alloc]initWithText:@"活动时间" size:AL(PT(26)) weight:UIFontWeightBold];
        _time_titleLBL.textColor = ColorHex(0x333333);
        [_scrollview addSubview:_time_titleLBL];
        
        [_time_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_detailTitleLBL.mas_bottom).with.offset(AL(PT(35)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_time_titleLBL.bounds.size);
        }];
    }
    return _time_titleLBL;
}

- (CYLabel *)time_startLBL{
    if (!_time_startLBL) {
        NSString *text = [NSString stringWithFormat:@"开始时间: %@", _model.started_time];
        _time_startLBL = [[CYLabel alloc]initWithText:text size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        [_scrollview addSubview:_time_startLBL];
        
        [_time_startLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_time_titleLBL.mas_bottom).with.offset(AL(PT(12)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_time_startLBL.bounds.size);
        }];
    }
    
    return _time_startLBL;
}

- (CYLabel *)time_endLBL{
    if (!_time_endLBL) {
        NSString *text= [NSString stringWithFormat:@"截止时间: %@", _model.end_time];
        _time_endLBL = [[CYLabel alloc]initWithText:text size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        [_scrollview addSubview:_time_endLBL];
        
        [_time_endLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_time_startLBL.mas_bottom).with.offset(AL(PT(10)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_time_endLBL.bounds.size);
        }];
    }
    
    return _time_endLBL;
}
/*活动说明*/
- (CYLabel *)activity_explain_titleLBL{
    if (!_activity_explain_titleLBL) {
        _activity_explain_titleLBL = [[CYLabel alloc]initWithText:@"活动说明" size:AL(PT(26)) weight:UIFontWeightBold];
        _activity_explain_titleLBL.textColor = [UIColor blackColor];
        [_scrollview addSubview:_activity_explain_titleLBL];
        
        [_activity_explain_titleLBL  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_time_endLBL.mas_bottom).with.offset(AL(PT(35)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_activity_explain_titleLBL.bounds.size);
        }];
    }
    
    return _activity_explain_titleLBL;
}

- (CYLabel *)activity_explain_contentLBL{
    if (!_activity_explain_contentLBL) {
        _activity_explain_contentLBL = [[CYLabel alloc]initWithText:[_model getStringWith:_model.active_content] size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        _activity_explain_contentLBL.numberOfLines = 0;
        [_scrollview addSubview:_activity_explain_contentLBL];
        
        [_activity_explain_contentLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_activity_explain_titleLBL.mas_bottom).with.offset(AL(PT(12)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo([_activity_explain_contentLBL autoSizeWithWidth:(SCREEN_WIDTH - AL(PT(30 * 2)))]);
        }];
    }
    
    return _activity_explain_contentLBL;
}
/*活动规则*/
- (CYLabel *)activity_rule_titleLBL{
    if (!_activity_rule_titleLBL) {
        _activity_rule_titleLBL = [[CYLabel alloc]initWithText:@"活动规则" size:AL(PT(26)) weight:UIFontWeightBold];
        [_scrollview addSubview:_activity_rule_titleLBL];
        
        [_activity_rule_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_activity_explain_contentLBL.mas_bottom).with.offset(AL(PT(35)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_activity_rule_titleLBL.bounds.size);
        }];
    }
    
    return _activity_rule_titleLBL;
}

- (CYLabel *)activity_rule_contentLBL{
    if (!_activity_rule_contentLBL) {
        _activity_rule_contentLBL = [[CYLabel alloc]initWithText:[_model getStringWith:_model.activity_rule] size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        _activity_rule_contentLBL.numberOfLines = 0;
        [_scrollview addSubview:_activity_rule_contentLBL];
        
        [_activity_rule_contentLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_activity_rule_titleLBL.mas_bottom).with.offset(AL(PT(12)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo([_activity_rule_contentLBL autoSizeWithWidth:(SCREEN_WIDTH - AL(PT(30 * 2)))]);
        }];
    }
    return _activity_rule_contentLBL;
}

- (CYLabel *)activity_ownershipLBL{
    if (!_activity_ownershipLBL) {
        _activity_ownershipLBL = [[CYLabel alloc]initWithText:_model.activityOwnership size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        _activity_ownershipLBL.numberOfLines = 0;
        [_scrollview addSubview:_activity_ownershipLBL];
        
        [_activity_ownershipLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_activity_rule_contentLBL.mas_bottom).with.offset(AL(PT(25)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo([_activity_ownershipLBL autoSizeWithWidth:(SCREEN_WIDTH - AL(PT(30 * 2)))]);
        }];
    }
    return _activity_ownershipLBL;
}

- (CYLabel *)download_titleLBL{
    if (!_download_titleLBL) {
        _download_titleLBL = [[CYLabel alloc]initWithText:@"下载彩运彩票手机APP"size:AL(PT(26)) weight:UIFontWeightBold];
        [_scrollview addSubview:_download_titleLBL];
        
        [_download_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_activity_ownershipLBL.mas_bottom).with.offset(AL(35));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_download_titleLBL.bounds.size);
        }];
    }
    return _download_titleLBL;
}

- (CYLabel *)download_appleLBL{
    if (!_download_appleLBL) {
        _download_appleLBL = [[CYLabel alloc]initWithText:@"苹果IOS下载网址:" size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        [_scrollview addSubview:_download_appleLBL];
        
        [_download_appleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_download_titleLBL.mas_bottom).with.offset(AL(PT(12)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_download_appleLBL.bounds.size);
        }];
    }
    return _download_appleLBL;
}

- (CYButton *)link_appleBtn{
    if (!_link_appleBtn) {
        _link_appleBtn = [[CYButton alloc]initWithTitle:_model.ios_download titleColor:BLUE_COLOR titleSize:AL(PT(22)) Image:nil];
        [_link_appleBtn setTitleColor:ColorHex(0x888888) forState:UIControlStateHighlighted];
        [_link_appleBtn addTarget:self action:@selector(actionForLinkAppleBtn) forControlEvents:UIControlEventTouchUpInside];
        [_scrollview addSubview:_link_appleBtn];
        
        [_link_appleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_download_appleLBL.mas_bottom).with.offset(AL(PT(12)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo([_link_appleBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:_link_appleBtn.titleLabel.font}]);
        }];
        
    }
    return _link_appleBtn;
}

- (CYLabel *)download_androidLBL{
    if (!_download_androidLBL) {
        _download_androidLBL = [[CYLabel alloc]initWithText:@"安卓android下载网址:" size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        [_scrollview addSubview:_download_androidLBL];
        
        [_download_androidLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_link_appleBtn.mas_bottom).with.offset(AL(PT(35)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo(_download_androidLBL.bounds.size);
        }];
    }
    return _download_androidLBL;
}

- (CYButton *)link_androidBtn{
    if (!_link_androidBtn) {
        _link_androidBtn = [[CYButton alloc]initWithTitle:_model.android_download titleColor:BLUE_COLOR titleSize:AL(PT(22)) Image:nil];
        [_link_androidBtn setTitleColor:ColorHex(0x888888) forState:UIControlStateHighlighted];
        [_link_androidBtn addTarget:self action:@selector(actionForLinkAndroidBtn) forControlEvents:UIControlEventTouchUpInside];
        [_scrollview addSubview:_link_androidBtn];
        
        WS(_scrollview)
        [_link_androidBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_download_androidLBL.mas_bottom).with.offset(AL(PT(12)));
            make.bottom.equalTo(weakSelf.mas_bottom).with.offset(-AL(PT(12)));
            make.left.equalTo(_titleLBL);
            make.size.mas_equalTo([_link_androidBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:_link_androidBtn.titleLabel.font}]);
        }];
        
    }
    return _link_androidBtn;
    
}

- (CYButton *)registerBtn{
    if (!_registerBtn) {
        _registerBtn = [[CYButton alloc]initWithTitle:@"去登录" titleColor:ColorHex(0xff5b05) titleSize:AL(PT(32)) Image:nil];
        [_registerBtn addTarget:self action:@selector(actionForRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
        [_registerBtn setTitleColor:ColorHex(0x888888) forState:UIControlStateHighlighted];
        _registerBtn.backgroundColor = ColorHex(0xf5f5f9);
        [self.view addSubview:_registerBtn];
        
        WS(self)
        [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view).with.offset(0);
            make.bottom.equalTo(weakSelf.view.mas_bottom).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2.0, AL(PT(98))));
        }];
        
    }
    return _registerBtn;
}

- (CYButton *)rechargeBtn{
    if (!_rechargeBtn) {
        _rechargeBtn = [[CYButton alloc]initWithTitle:@"去充值" titleColor:ColorHex(0xff5b05) titleSize:AL(PT(32)) Image:nil];
        [_rechargeBtn addTarget:self action:@selector(actionForRechargeBtn) forControlEvents:UIControlEventTouchUpInside];
        [_rechargeBtn setTitleColor:ColorHex(0x888888) forState:UIControlStateHighlighted];
        _rechargeBtn.backgroundColor = ColorHex(0xf5f5f9);
        [self.view addSubview:_rechargeBtn];
        
        WS(self)
        [_rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.view.mas_right).with.offset(0);
            make.bottom.equalTo(weakSelf.view.mas_bottom).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2.0, AL(PT(98))));
        }];
        
    }
    return _rechargeBtn;
}

- (CYView *)midLine{
    if (!_midLine) {
        _midLine = [[CYView alloc]initWithColor:ColorHex(0xd4d4d4)];
        [self.view addSubview:_midLine];
        
        WS(self)
        [_midLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.view);
            make.bottom.equalTo(weakSelf.view.mas_bottom).with.offset(- AL(PT((98 - 70) / 2.0)));
            make.size.mas_equalTo(CGSizeMake(0.5, AL(PT(70))));
            
        }];
    }
    return _midLine;
}

- (CYView *)topLine{
    if (!_topLine) {
        _topLine = [[CYView alloc]initWithColor:ColorHex(0xe8e8e8)];
        [self.view addSubview:_topLine];
        
        WS(self)
        [_topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.view);
            make.bottom.equalTo(weakSelf.view).with.offset(- AL(PT(98 + 1)));
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, AL(PT(1))));
        }];
    }
    
    return _topLine;
}

- (void)setModel:(CYActivityModel *)model{
    if (_model != model)
        _model = model;
    [self setupSource];
}

- (void)setupSource{
    self.scrollview.hidden = NO;
    self.titleLBL.hidden = self.IMG.hidden = self.detailTitleLBL.hidden = self.time_titleLBL.hidden = self.time_startLBL.hidden = self.time_endLBL.hidden = NO;
    self.activity_explain_titleLBL.hidden = self.activity_explain_contentLBL.hidden = self.activity_rule_titleLBL.hidden =self.activity_rule_contentLBL.hidden = NO;
    self.activity_ownershipLBL.hidden = self.download_titleLBL.hidden = self.download_appleLBL.hidden = self.link_appleBtn.hidden = self.download_androidLBL.hidden = self.link_androidBtn.hidden = NO;
    
    [CYHttps loadingWithFile:_model.mobile_img_url complete:^(id data) {
        _IMG.loadingView.hidden = YES;
        _IMG.image = data;
    }];
}

#pragma mark - 按钮事件
- (void)actionForLinkAppleBtn{
    
}
- (void)actionForLinkAndroidBtn{
    
}

- (void)actionForRegisterBtn{
    
    if ([[CYUser shareUser] username] || [[CYUser shareUser] token])
        [CYHUD show:@"你已登录"];
    else {
        Class VC = NSClassFromString(@"CYLoginViewController");
        [self.navigationController pushViewController:[VC new] animated:YES];
    }
}

- (void)actionForRechargeBtn{
    Class VC = NSClassFromString(@"CYRechargeWayViewController");
    [self.navigationController pushViewController:[VC new] animated:YES];
}

- (void)https{
    
  self.task = [CYHttps POST:API_activity_detail parameters:@{@"id":@(_datail_id)} showHUD:YES progress:nil success:^(id data) {
        if (data)
            self.model = [CYActivityModel getModel:data];
        else
            [self performSelector:@selector(back)];
        
    } failure:nil];
    
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = @"优惠活动";
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self https];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
